<?php

namespace App\Services\Core\Store\Qiniu;


use App\Helpers\Logger\SLogger;
use App\Models\Orm\PlatformProduct;
use App\Services\Core\Store\Hengxin\HengxinConfig;
use App\Services\Core\Store\Wechat\JssdkService;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;


/**
 * Class QiniuService
 * @package App\Services\Core\Qiniu
 * 七牛
 */
class QiniuService
{
    public static  function uploadImage($media_id)
    {
        $bb= new JssdkService("wx99c7bf3c8db07fb4","feade0d5fc3e5b6924c1de40b40389c5",$media_id);

        $mediaContent = $bb->aa($media_id);
        $fetch =base64_encode($mediaContent);
        $accessKey = QiniuConfig::ACCESSKEY;
        $secretKey =  QiniuConfig::SECRETKEY;
        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        $bucket = QiniuConfig::BUCKET;
        $policy =array('mimeLimit' => 'image/*');
        // 生成上传 Token
        $token = $auth->uploadToken($bucket, $key = null, $expires = 3600, $policy, $strictPolicy = true,$zone = null);

        $imggg1 =  self::request_by_curl('http://upload-z1.qiniu.com/putb64/-1',$fetch,$token);
        $imgs1 = json_decode(trim($imggg1),true);

        $imgss1 = $imgs1['hash'];
        return  $imgss1;//存数据库的地址

    }


    public static function request_by_curl($remote_server,$post_string,$token) {

        $headers = array();
        $headers[] = 'Content-Type:image/png';
        $headers[] = 'Authorization:UpToken '.$token;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$remote_server);
        //curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER ,$headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }

    public static  function delImage($key){
        $accessKey = QiniuConfig::ACCESSKEY;
        $secretKey =  QiniuConfig::SECRETKEY;
        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        $bucket = QiniuConfig::BUCKET;

        $bucketMgr = new BucketManager($auth);

        //你要测试的空间， 并且这个key在你空间中存在

        //删除$bucket 中的文件 $key
        $err = $bucketMgr->delete($bucket, $key);


    }

    //上传到七牛
    public static  function addImage($fetch)
    {

        $accessKey = QiniuConfig::ACCESSKEY;
        $secretKey =  QiniuConfig::SECRETKEY;
        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        $bucket = HengxinConfig::BUCKET;
        $policy =array('mimeLimit' => 'image/*');
        // 生成上传 Token
        $token = $auth->uploadToken($bucket, $key = null, $expires = 3600, $policy, $strictPolicy = true,$zone = null);

        $imggg1 =  self::request_by_curl('http://upload-z0.qiniu.com/putb64/-1',$fetch,$token);
        if(strpos($imggg1,'error') == true)
        {
            return false;
        }
        $imgs1 = json_decode(trim($imggg1),true);
        $imgss1 = $imgs1['hash'];
        return  $imgss1;//存数据库的地址

    }


}
